<template>
  <div class="todoapp">
    <TodoHeader :arr="showList" @addTest="addT"></TodoHeader>
    <TodoMain :arr="showList" @del="delen"></TodoMain>
    <TodoFooter
      :arr="showList"
      @changeEv="changeFn"
      @clearEvent="clearFn"
    ></TodoFooter>
  </div>
</template>

<script>
// 引入样式
import "./styles/base.css";
import "./styles/index.css";
// 引入组件
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

export default {
  // 注册组件
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: [
        { id: 100, name: "吃饭", isDone: true },
        { id: 101, name: "睡觉", isDone: false },
        { id: 102, name: "打豆豆", isDone: true },
      ],
      curType: "total"
    };
  },
  methods: {
    addT(name) {
      if (!name) {
        return alert("输入内容为空！");
      }
      let id =
        this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 100;
      this.list.push({
        id: id,
        name: name,
        isDone: false,
      });
    },
    delen(index) {
      // 删除对应索引得数组元素
      this.list.splice(index, 1);
    },
    changeFn(type) {
      this.curType = type;
    },
    clearFn() {
      this.list = this.list.filter((item) => {
        return item.isDone === false;
      });
    },
  },
  computed: {
    showList() {
      if (this.curType === "yes") {
        let newArr = this.list.filter((item) => {
          return item.isDone === true;
        });
        return newArr;
      } else if (this.curType === "no") {
        return this.list.filter((item) => {
          return item.isDone === false;
        });
      } else {
        return this.list;
      }
    },
  },
};
</script>

<style>
</style>